<?php

class Security
{
	private static $openssl = array(
			'method'=>'AES-256-CBC',
			'password'=>'Cualquier contraseña para OpenSSL.',
		);

	public static function encrypt($text)
	{
		$method = self::$openssl['method'];
		$password = self::$openssl['password'];
		$ivLength = openssl_cipher_iv_length($method);
		$iv = openssl_random_pseudo_bytes($ivLength);
		$encrypted = openssl_encrypt($text, $method, $password, 0, $iv);
		return base64_encode($iv.$encrypted);
	}

	public static function decrypt($text)
	{
		$text = base64_decode($text);
		$method = self::$openssl['method'];
		$password = self::$openssl['password'];
		$ivLength = openssl_cipher_iv_length($method);
		$encrypted = substr($text, $ivLength);
		$iv = substr($text, 0, $ivLength);
		return @openssl_decrypt($encrypted, $method, $password, 0, $iv);
	}
}